﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRM.BIZ;
using HRM.Utility;
using HRM.Entity;

namespace WebUI.UserControls
{
    public partial class EmployeeListControl : System.Web.UI.UserControl
    {
        private static object dataSource = null;

        public object EmployeeDataSource
        {
            get
            {
                return dataSource;
            }
            set
            {
                dataSource = value;
            }
        }

        public void BindData()
        {
            try
            {
                grvEmployee.DataSource = EmployeeDataSource;
                grvEmployee.DataBind();
            }
            catch (Exception ex)
            {
                grvEmployee.DataSource = null;
                grvEmployee.DataBind();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void grvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                /*int positionID = e.Row.Cells[5].Text.ToInt32();
                int departmentID = e.Row.Cells[6].Text.ToInt32();

                e.Row.Cells[5].Text = new PositionBiz().GetByID(positionID).Name;
                e.Row.Cells[6].Text = new DepartmentBiz().GetByID(departmentID).Name;*/
                bool isActive = e.Row.Cells[7].Text.ToInt32().ToBool();
                if (isActive)
                {
                    e.Row.Cells[7].Text = "Đang làm việc";
                    //HtmlControl control = e.Row.FindControl("divAction") as HtmlControl;
                    //control.Style.Add("visibility", "visible");
                }
                else
                {
                    e.Row.Cells[7].Text = "Đã nghỉ việc";
                    //HtmlControl control = e.Row.FindControl("divAction") as HtmlControl;
                    //control.Style.Add("visibility", "hidden");
                }

            }
            else if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox chk = e.Row.FindControl("chkAll") as CheckBox;
                if (chk != null)
                {
                    chk.Attributes.Add("onclick", string.Format("selectAll('{0}')", chk.ClientID));
                }
            }
        }

        protected void grvEmployee_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox chkAll = new CheckBox();
                chkAll.ID = "chkAll";
                e.Row.Cells[0].Controls.Add(chkAll);
            }
        }

        protected void grvEmployee_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "EditEmp":
                    {
                        string id = e.CommandArgument.ToString();
                        Response.Redirect("EditEmployee.aspx?id=" + id);
                        break;
                    }
                case "DeleteEmp":
                    {
                        int id = e.CommandArgument.ToString().ToInt32();
                        EmployeeBiz empBiz = new EmployeeBiz();
                        Employee emp = empBiz.GetByID(id);
                        emp.Status = false;
                        empBiz.Update(emp);
                        BindData();
                        break;
                    }
                case "ViewEmp":
                    {
                        string id = e.CommandArgument.ToString();
                        Response.Redirect("ViewEmployee.aspx?id=" + id);
                        break;
                    }
                case "EmployeeContract":
                    {
                        string id = e.CommandArgument.ToString();
                        Response.Redirect("EmployeeContract.aspx?id=" + id);
                        break;
                    }
                case "EmployeeReward":
                    {
                        string id = e.CommandArgument.ToString();
                        Response.Redirect(string.Format("EmployeeReward.aspx?id={0}&type={1}", id, 1));
                        break;
                    }
                case "EmployeeDiscipline":
                    {
                        string id = e.CommandArgument.ToString();
                        Response.Redirect(string.Format("EmployeeReward.aspx?id={0}&type={1}", id, -1));
                        break;
                    }
                case "EmployeeLeave":
                    {
                        string id = e.CommandArgument.ToString();
                        Response.Redirect(string.Format("EmployeeLeave.aspx?id={0}", id));
                        break;
                    }
            }
        }

        protected void grvEmployee_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (EmployeeDataSource != null)
            {
                grvEmployee.PageIndex = e.NewPageIndex;
                BindData();
            }
        }
    }
}